<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/chartregister/index
 */
class Chartregister extends CsmBackend
{

    /**
     */
    protected $model = null;


    public function index()
    {
        //1.0.2 修复了HTTPS无法打开地图的问题@chensm
        //header("Content-Security-Policy: upgrade-insecure-requests");
        // $isrefresh = $this->csmreq("isrefresh", false);
        $today = date("Y-m-d");
        $yestoday = date("Y-m-d", strtotime("-1 day"));

        // 是否强制刷新
        if (true) {
            CsmonlineCron::crononedate($today, true);
            $this->view->assign("baseurl", $this->request->baseUrl());
            CsmonlineCron::crononedate($yestoday, false);
        }

        // 统计截止时间
        if (true) {
            $dao = new \app\admin\model\csmonline\Crontab();
            $row = $dao->where("statdate", "=", $today)->find();

            $this->view->assign("todaystat", ($row == null) ? "今天无数据" : date("Y-m-d H:i", $row->starttime));
        }

        // 今日新增 和 总注册数
        if (true) {
            $dao = new \app\admin\model\csmonline\Piestatdate();
            $today = $dao->where("statdate", "=", $today)->find();
            $yestoday = $dao->where("statdate", "=", $yestoday)->find();
            $this->view->assign("today", $today);
            $this->view->assign("yestoday", $yestoday);
        }
        // // 在线人数,2分钟无访问，则认为离线
        // if (true) {
        // // $dao = new \app\admin\model\csmonline\Firstid();
        // // $row = $dao->where("statdate", "=", $today)->find();
        // $firstid = CsmonlineCron::getUserLogFirstId($today);
        // $daolog = new \app\admin\model\csmonline\Userlog();
        // $onlinen = $daolog->where("id", ">=", $firstid)
        // ->where("isloginuser", "=", "N")
        // ->where("durationlasttime", "<", time() + 2 * 60)
        // ->count("distinct userid");
        // $onliney = $daolog->where("id", ">=", $firstid)
        // ->where("isloginuser", "=", "Y")
        // ->where("durationlasttime", "<", time() + 2 * 60)
        // ->count("distinct userid");
        // $this->view->assign("onlinen", $onlinen);
        // $this->view->assign("onliney", $onliney);
        // }

        return $this->view->fetch();
    }

    // 趋势图
    public function chart1()
    {
        $begin = $this->csmreq("begin", true);
        $end = $this->csmreq("end", true);

        $begindate = strtotime($begin);
        $enddate = strtotime($end);
        $calcday = ($enddate - $begindate) / 24 / 3600;
        $allregisteredus = [];
        $newregisteredus = [];
        $dau = [];
        for ($i = 0; $i <= $calcday; $i ++) {
            $dd = date("Y-m-d", $begindate);
            $allregisteredus[$dd] = [
                $dd,
                0,
                0
            ];
            $newregisteredus[$dd] = [
                $dd,
                0,
                0
            ];
            $dau[$dd] = [
                $dd,
                0,
                0
            ];
            $begindate = $begindate + 3600 * 24;
        }

        $dao = new \app\admin\model\csmonline\Piestatdate();
        $list = $dao->where("statdate", ">=", $begin)
            ->where("statdate", "<=", $end)
            ->select();
        foreach ($list as $v) {
            $allregisteredus[$v->statdate][1] = $v->allregisteredus;
            $newregisteredus[$v->statdate][1] = $v->newregisteredus;
            $dau[$v->statdate][1] = $v->dau;
        }

        // array_unshift($ips,['product', '游客统计', '登录用户统计']);
        $this->success("", null, array(
            "allregisteredus" => $this->adjust($allregisteredus),
            "newregisteredus" => $this->adjust($newregisteredus),
            "dau" => $this->adjust($dau)
        ));
    }

    private function adjust($list)
    {
        $ll = [];
        foreach ($list as $v) {
            $ll[] = $v;
        }
        array_unshift($ll, [
            'product',
            '游客统计',
            '登录用户统计'
        ]);
        return $ll;
    }

    // 近期注册（实时）
    public function lastregister()
    {
        CsmonlineCron::generatefauser();
        $dao = new \app\admin\model\csmonline\Fauser();
        $list = $dao->alias('t')
            ->join('user a', 't.user_id=a.id', 'left')
            ->field('a.id,a.username,a.nickname,a.email,a.mobile,t.country,t.province,t.jointime')
            ->order('t.jointime', 'desc')
            ->limit(0, 10)
            ->select();
        $this->view->assign("list", $list);
        return $this->view->fetch();
    }
}

